﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class AB05AV95 : DocumentBase
    {
        #region-------------Properties-----------------------
        int ThreadCount = 0;
        int finishedcount = 0;
        int totalrecord = 0;
        string myConnString="";
        DataTable tableUI;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public INTEXDataSet setUI;
         
        public INTEXDataSet setWorker = new INTEXDataSet();
        AB05AV95Data m_Data, m_DataX;
        public DataSet setResult;
        BackgroundWorker bWork = new BackgroundWorker();
        CollectionViewSource vwABPOS;
        
        private DataRow dwUI;
        public AB05AV95()
        {
            InitializeComponent();
            this.Name = "后整理生产浏览";
            this.Title = "后整理生产浏览";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent(); 
            this.dicParamater = dicParamater; 
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"]; 
            m_Data = new AB05AV95Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy; 
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("ABPERLKZ", typeof(string));
            tableUI.Columns.Add("ABPAUFKRS", typeof(string));
            tableUI.Columns.Add("ABPAUFLFD", typeof(string));
            tableUI.Columns.Add("ABPAUFPOS", typeof(string));
            tableUI.Columns.Add("LSNAME1", typeof(string));
            tableUI.Columns.Add("WBBTP", typeof(string));
            tableUI.Columns.Add("ABPZUGET", typeof(string));
            tableUI.Columns.Add("ABPART", typeof(string));
            tableUI.Columns.Add("GISSUEDATE", typeof(string));
            tableUI.Columns.Add("FGETDATE", typeof(string)); 
            vwABPOS = (CollectionViewSource)TryFindResource("vwABPOS");
            setUI=(INTEXDataSet)TryFindResource("iNTEXDataSet");
        }
        #endregion

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.Rows.Add();
                finishedcount = 0;
                setUI.Clear();
                zoneQueryCond.IsExpanded = false;
                setUI.AcceptChanges();
                string KRS = txtKRS.Text.Trim();
                string LFD = txtLFD.Text.Trim();
                string POS = txtPOS.Text.Trim();
                string LOT = txtLOT.Text.Trim();
                string ERLKZ = txtERLKZ.IsChecked.Value.ToString();
                if (ERLKZ == "True")
                {
                    dwUI["ABPERLKZ"] = "J";
                }
                else
                {
                    dwUI["ABPERLKZ"] = " ";
                }

                dwUI["ABPAUFKRS"] = KRS;
                dwUI["ABPAUFLFD"] = "";
                if (LFD.Trim().Length > 0)
                {
                    dwUI["ABPAUFLFD"] = m_Data.Right("000000" + LFD, 6);
                }
                dwUI["ABPAUFPOS"] = "";
                if (POS.Trim().Length > 0)
                {
                    dwUI["ABPAUFPOS"] = m_Data.Right("000" + POS, 3);
                }
                dwUI["ABPZUGET"] = "0";
                dwUI["ABPART"] = txtArticle.Text.ToString().Trim();
                string FACTORY = txtFACTORY.Text.ToString().Trim();
                if (FACTORY.Trim().Length > 0)
                {
                    dwUI["LSNAME1"] = m_Data.Right("000000" + FACTORY, 8);
                }
                string GISSUEStartDate = gsendstartdate.Text.ToString().Trim();
                string GISSUEEndDate = gsendenddate.Text.ToString().Trim();
                if (GISSUEStartDate == "")
                {
                    GISSUEStartDate = "20000101";
                }
                if (GISSUEEndDate == "")
                {
                    GISSUEEndDate = "29990101";
                }
                dwUI["GISSUEDATE"] = GISSUEStartDate;
                dwUI["FGETDATE"] = GISSUEEndDate;
                using (new LongOperation(this, "读取符合条件的记录"))
                {


                    if (txtBYLOT.IsChecked == false)
                    {
                        if (LFD == "")
                        {
                            setResult = m_Data.GetABPOS(dwUI);
                        }
                        else
                        {
                            setResult = m_Data.GetABPOSBYABPLFD(dwUI);
                        }
                    }
                    else
                    {
                        dwUI["WBBTP"] = LOT;
                        setResult = m_Data.GetABPOSBYLOT(ref setUI, dwUI);
                    }
                    totalrecord = setUI.ABPOS.Rows.Count;
                }
                vwABPOS.View.MoveCurrentToFirst();
                
                foreach (DataRow dwNABPOS in setUI.ABPOS)
                {
                    ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage("AB05AV95Data", dwNABPOS,OracleDB,proxy,null);
                }
                return;
            }
            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "数据查询中"))
                {
                    dateChooser = (DatePicker)this.FindName("DateChooser");
                    
                    if (setUI != null)
                    {
                        //setResult = m_Data.GetWASTAMM();
                        //setUI.Merge(setResult);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }
        private void gsendstartDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendstartdate.Text = m_Data.GetDate(sender);
        }
        private void gsendendDateChooser_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            gsendenddate.Text = m_Data.GetDate(sender);
        }
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                m_DataX = new AB05AV95Data(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                AB05AV95Report m_rpt = new AB05AV95Report(dicParamater);
                m_rpt.dsSource = setUI;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void txt_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            //tb.Text = "";
        }

        private void dlgABPOS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataRowView drv = null;
            ItemsControl sd;
            ICollectionView view;
            try
            {

                sd = sender as ItemsControl;
                view = CollectionViewSource.GetDefaultView(sd.ItemsSource);
                if (e.AddedItems.Count > 0)
                {
                    drv = e.AddedItems[0] as DataRowView;
                    DataRow dwTemp = drv.Row;
                    //m_Data.GetABFOLGE(dwTemp);
                    //m_Data.GetGWBORDER(dwTemp); 
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Background, (ThreadStart)delegate
                    {
            
                    }
                    );
                    /*
                    m_Data = new AB05AV95Data(OracleDB,proxy);
                    m_Data.ThreadID = ThreadCount++;
                    EventDispatcher.Instance.RaiseUIMessage(m_Data, dwTemp
                       
                    ); */
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            return;
            bool boolGo = true;
            while (!vwABPOS.View.IsCurrentAfterLast)
            {
                if (ThreadCount == 10)
                {
                    boolGo = false;
                    if (EventDispatcher.Instance.FinishedJobs == ThreadCount) 
                    {
                        boolGo = true;
                        ThreadCount = 0;
                        EventDispatcher.Instance.FinishedJobs = 0;
                    }
                }
                if (boolGo)
                {
                    vwABPOS.View.MoveCurrentToNext();
                }
            }
        }
    }
}
